import * as CryptoJS from 'crypto-js'

//加密处理
export const encryption = (params) => {
	let {
		data,
		type,
		param,
		key
	} = params;
	const result = JSON.parse(JSON.stringify(data));
	if (type === 'Base64') {
		param.forEach(ele => {
			result[ele] = btoa(result[ele])
		})
	} else {
		param.forEach(ele => {
			var data = result[ele]
			key = CryptoJS.enc.Latin1.parse(key)
			var iv = key
			// 加密
			var encrypted = CryptoJS.AES.encrypt(
				data,
				key, {
					iv: iv,
					mode: CryptoJS.mode.CBC,
					padding: CryptoJS.pad.ZeroPadding
				})
			result[ele] = encrypted.toString()
		})
	}
	return result
}

export const FilterNullValue = (obj, fixed = 8) => {
  //检验null和undefined
  if (!obj && obj !== 0/*  && obj !== '' */) {
    return false
  }
  //检验数组
  if (Array.prototype.isPrototypeOf(obj) && obj.length === 0) {
    return false
  }
  //检验对象
  if (Object.prototype.isPrototypeOf(obj) && Object.keys(obj).length === 0) {
    return false
  }

  if (typeof obj === 'number') {
    obj = obj.toFixed(fixed) * 1
  }
  return obj
}

//生成随机len位数字
export const randomLenNum = (len, date) => {
	let random = ''
	random = Math.ceil(Math.random() * 100000000000000).toString().substr(0, len || 4)
	if (date) random = random + Date.now()
	return random
}

//截取url地址上的参数
export function getUrlData(strName) {
	var urlStr = location.href;
	var index = urlStr.indexOf('?');
	var paramStr = urlStr.substr(index + 1);
	var paramArr = paramStr.split('&');
	for (var i = 0; i < paramArr.length; i++) {
		var param = paramArr[i].split("=");
		if (param[0].toUpperCase() == strName.toUpperCase()) {
			return param[1];
		}
	}
}


let x_PI = 3.14159265358979324 * 3000.0 / 180.0;
let PI = 3.1415926535897932384626;
let a = 6378245.0;
let ee = 0.00669342162296594323;
/**
 * WGS84转GCj02
 */

export function wgs84togcj02(lng, lat) {
	let dlat = transformlat(lng - 105.0, lat - 35.0);
	let dlng = transformlng(lng - 105.0, lat - 35.0);
	let radlat = lat / 180.0 * PI;
	let magic = Math.sin(radlat);
	magic = 1 - ee * magic * magic;
	let sqrtmagic = Math.sqrt(magic);
	dlat = (dlat * 180.0) / ((a * (1 - ee)) / (magic * sqrtmagic) * PI);
	dlng = (dlng * 180.0) / (a / sqrtmagic * Math.cos(radlat) * PI);
	let mglat = lat + dlat;
	let mglng = lng + dlng;
	return [mglng, mglat]
}

export function gcj02towgs84(lng,lat) {
	let dlat = transformlat(lng - 105.0, lat - 35.0);
	let dlng = transformlng(lng - 105.0, lat - 35.0);
	let radlat = lat / 180.0 * PI;
	let magic = Math.sin(radlat);
	magic = 1 - ee * magic * magic;
	let sqrtmagic = Math.sqrt(magic);
	dlat = (dlat * 180.0) / ((a * (1 - ee)) / (magic * sqrtmagic) * PI);
	dlng = (dlng * 180.0) / (a / sqrtmagic * Math.cos(radlat) * PI);
	let mglat = lat + dlat;
	let mglng = lng + dlng;
	return [lng * 2 - mglng, lat * 2 - mglat]
}

function transformlat(lng, lat) {
	let ret = -100.0 + 2.0 * lng + 3.0 * lat + 0.2 * lat * lat + 0.1 * lng * lat + 0.2 * Math.sqrt(Math.abs(lng));
	ret += (20.0 * Math.sin(6.0 * lng * PI) + 20.0 * Math.sin(2.0 * lng * PI)) * 2.0 / 3.0;
	ret += (20.0 * Math.sin(lat * PI) + 40.0 * Math.sin(lat / 3.0 * PI)) * 2.0 / 3.0;
	ret += (160.0 * Math.sin(lat / 12.0 * PI) + 320 * Math.sin(lat * PI / 30.0)) * 2.0 / 3.0;
	return ret
}

function transformlng(lng, lat) {
	let ret = 300.0 + lng + 2.0 * lat + 0.1 * lng * lng + 0.1 * lng * lat + 0.1 * Math.sqrt(Math.abs(lng));
	ret += (20.0 * Math.sin(6.0 * lng * PI) + 20.0 * Math.sin(2.0 * lng * PI)) * 2.0 / 3.0;
	ret += (20.0 * Math.sin(lng * PI) + 40.0 * Math.sin(lng / 3.0 * PI)) * 2.0 / 3.0;
	ret += (150.0 * Math.sin(lng / 12.0 * PI) + 300.0 * Math.sin(lng / 30.0 * PI)) * 2.0 / 3.0;
	return ret
}





